package com.luocc.ccdemo.ui

import android.app.AlertDialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.luocc.ccdemo.R
import com.luocc.ccdemo.entity.User
import com.luocc.ccdemo.vm.UserViewModel
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

class LoginFragment : Fragment() {
    private lateinit var mContext: Context
    private lateinit var mUsername: EditText
    private lateinit var mPassword: EditText
    private lateinit var mSavePassword: CheckBox
    private lateinit var mUserViewModel: UserViewModel
    private var mProgressDialog: AlertDialog? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java)
        mUserViewModel.getLoginResult().observe(this) {
            mProgressDialog?.cancel()
            if (it.code == 0) {
                findNavController().navigate(R.id.action_loginFragment_to_homeFragment)
                val sp = mContext.getSharedPreferences("user", Context.MODE_PRIVATE)
                val edit = sp.edit()
                if (mSavePassword.isChecked) {
                    edit.putString("username", mUsername.text.toString())
                    edit.putString("password", mPassword.text.toString())
                } else {
                    edit.remove("username")
                    edit.remove("password")
                }
                edit.putBoolean("save_password", mSavePassword.isChecked)
                edit.apply()
            } else {
                Toast.makeText(mContext, "登录失败:${it.message}", Toast.LENGTH_LONG).show()
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val parent = inflater.inflate(R.layout.fragment_login, container, false)
        mUsername = parent.findViewById(R.id.username)
        mPassword = parent.findViewById(R.id.password)
        val login = parent.findViewById<Button>(R.id.login)
        mSavePassword = parent.findViewById(R.id.save_password)
        mProgressDialog = AlertDialog.Builder(context)
            .setView(R.layout.layout_dialog_progress)
            .setCancelable(false)
            .create()
        val toRegister = parent.findViewById<Button>(R.id.to_register)

        login.setOnClickListener {
            mProgressDialog?.show()
            val user = User(mUsername.text.toString(), mPassword.text.toString())
            mUserViewModel.login(user)
        }

        toRegister.setOnClickListener {
            findNavController().navigate(R.id.action_loginFragment_to_registerFragment)
        }

        val sp = mContext.getSharedPreferences("user", Context.MODE_PRIVATE)
        val savePassword = sp.getBoolean("save_password", false)
        if (savePassword) {
            val username = sp.getString("username", "")
            val password = sp.getString("password", "")
            mSavePassword.isChecked = true
            mUsername.setText(username)
            mPassword.setText(password)
        }
        return parent
    }

    companion object {
        private const val TAG = "LC_LoginFragment"
    }
}